package Test_Web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BrowseBySingleGenre
 */
public class BrowseBySingleGenre extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BrowseBySingleGenre() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String genreId = request.getParameter("genreId");
		
		// Build SQL Query
		String command = "select movies.id, movies.title, movies.year, director" +
					     " from movies,genres,genres_in_movies" +
					     " where movies.id = movie_id AND genres.id = genre_id AND genres.id = '" + genreId +"'";

		 // Pass SQL Query as parameter to Movie List
		Cookie cookie = new Cookie("sqlQuery", command);
		cookie.setMaxAge(60*60);
		response.addCookie(cookie);
		// Pass SQL Query as parameter to Movie List
		String redirectURL = "MovieList";
		response.sendRedirect(redirectURL);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
